package com.tempjob.common.context;

/**
 * 用来维护数据的本地线程变量,方便业务方直接从本地线程变量中获取用户相关信息的数据
 */
public class AdminContextManage {

    private final static ThreadLocal<AdminContext> USER_LOCAL = new ThreadLocal();

    private final static ThreadLocal<String> LOCAL_EXTRA_TOKEN = new ThreadLocal();

    public static void reInit(AdminContext adminContext, String authorization) {
        USER_LOCAL.set(adminContext);
        LOCAL_EXTRA_TOKEN.set(authorization);
    }

    public static void reInit(String authorization) {
        USER_LOCAL.remove();
        LOCAL_EXTRA_TOKEN.set(authorization);
    }

    public static void resetUserInfo(AdminContext adminContext) {
        USER_LOCAL.remove();
        USER_LOCAL.set(adminContext);
    }

    public static void reInit() {
        USER_LOCAL.remove();
        LOCAL_EXTRA_TOKEN.remove();
    }

    public static AdminContext getContext() {
        return USER_LOCAL.get();
    }

    public static String getAuthorization() {
        return LOCAL_EXTRA_TOKEN.get();
    }

    public static void clearData() {
        USER_LOCAL.remove();
        LOCAL_EXTRA_TOKEN.remove();
    }
}
